package osifbot;

import java.sql.Connection;
import java.sql.DriverManager;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;



/**
 *
 *
 * 
 *    OSIFBOT
 *    Copyright (C) 2013  etchner jorge@devetch.com
 *
 *    This program is free software: you can  redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class InterfazBBDD {
    
    private  MysqlDataSource dataSource;
    private Connection conexion;
    private Statement stmt;
    
    public InterfazBBDD(String user, String password, String database, String host) {
        dataSource = new MysqlDataSource();
        dataSource.setUser(user);
        dataSource.setDatabaseName(database);
        dataSource.setServerName(host);   
        dataSource.setPassword(password);
    }
    
    public boolean isConnected() throws SQLException{
        if(conexion == null) {
            return false;
        }
        return conexion.isClosed();
    }
    
    public void connect() throws SQLException{
        conexion = dataSource.getConnection();
        stmt = conexion.createStatement();
    }
    
    public void disconnect() throws SQLException{
        conexion.close();
    }
    
    public void addUser(String nick, long id) throws SQLException{
        String sentencia = "INSERT INTO USUARIO VALUES("+id+",'"+nick+"')";      
        stmt.execute(sentencia);
    }
    
    public void addAcceso(String nick, String fecha, long id) throws SQLException{
        String sentencia = "INSERT INTO ACCESO VALUES('"+nick+"',"+fecha+","+id+")";
        stmt.execute(sentencia);
    }
    
    public void addPeticion(String nick, String ip) throws SQLException{
        Calendar now = Calendar.getInstance(); 
        int anyo = now.get(Calendar.YEAR);   
        int mes = now.get(Calendar.MONTH)+1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int min = now.get(Calendar.MINUTE);
        
        
        String sentencia = "INSERT INTO PETICION (NICK,DIA,MES,ANYO,HORA,MINUTO,IP) VALUES('"+nick+"',"+day+","+mes+","+anyo+","+hour+","+min+",'"+ip+"')";
        stmt.execute(sentencia);        
    }
    
    public void addAdmin(String nick, String password) throws SQLException{
        String sentencia = "INSERT INTO ADMINISTRADOR VALUES('"+nick+"','"+password+"')";
        stmt.execute(sentencia);
    }
    
    
    
    
    
}
